//
//  MHSystemMessageViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/7/14.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSystemMessageViewModel.h"
#import "MHSystemMessageModel.h"
#import "MHSystemMessageItemViewModel.h"
#import "MHOderDetailViewModel.h"
#import "MHInComeViewModel.h"
#import "MHCircleMemberViewModel.h"
#import "MHCircleInfo.h"
#import "MHAuditListViewModel.h"
@interface MHSystemMessageViewModel()
@property (nonatomic, readwrite, copy) NSArray *systemMessages;
@end

@implementation MHSystemMessageViewModel
-(void)initialize
{
    [super initialize];
    self.title = @"系统消息";
    self.shouldPullUpToLoadMore = YES;
    /// 没有数据时，隐藏底部刷新控件
    self.shouldEndRefreshingWithNoMoreData = NO;
    self.shouldPullDownToRefresh = YES;
    self.shouldPullUpToLoadMore = YES;
    
    @weakify(self);
    RAC(self, systemMessages) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, systemMessages) map:^(NSArray * systemMessages) {
        @strongify(self)
        return [self dataSourceWithRepeaters:systemMessages];
    }];
    
    
    
    self.didSelectCommand  = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        NSIndexPath *indexPath = input;
        MHSystemMessageModel *system =  self.systemMessages[indexPath.row];
        MHPushModel *pushModel = system.transferJosn;
        if (pushModel) {
            if (pushModel.busType == MHPusBusTypeMember) {
                
                [[[MHHTTPService sharedInstance] fetchCircleInfoWithCircleId:pushModel.circleId] subscribeNext:^(MHCircleInfo *info) {
                    MHAuditListViewModel *circleModel = [[MHAuditListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey: info}];
                    
                    [self.services pushViewModel:circleModel animated:YES];
                }];
                
            }else if (pushModel.busType == MHPusBusTypeIncome)
            {
                MHInComeViewModel *income = [[MHInComeViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey: @(1)}];
                income.title =@"收入明细";
                [self.services pushViewModel:income animated:YES];
            }else if (pushModel.busType == MHPusBusTypeTiXian)
            {
                
                MHOderDetailViewModel *orderDetail = [[MHOderDetailViewModel alloc] initWithServices:self.services params:nil];
                orderDetail.title =@"提现明细";
                [self.services pushViewModel:orderDetail animated:YES];
            }
        }
        
        return [RACSignal empty];
    }];
    
}

//-(instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params
//{
//    if (self = [super initWithServices:services params:params]) {
//        self.info = params[MHViewModelUtilKey];
//    }
//    return self;
//}


- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    
    NSArray * (^mapRepeaters)(NSArray *) = ^(NSArray *systemMessages) {
        if (page == 1) {
            /// 下拉刷新
        } else {
            /// 上拉加载
            systemMessages = @[(self.systemMessages ?: @[]).rac_sequence, systemMessages.rac_sequence].rac_sequence.flatten.array;
        }
        return systemMessages;
    };
    
//    return [self.services.client getSystemMgsPagedListWithPageNum:page pageSize:self.perPage];
    return [[self.services.client getSystemMgsPagedListWithPageNum:page pageSize:self.perPage] map:mapRepeaters];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithRepeaters:(NSArray *)systemMessages {
//    MHCircleMember *all = [[MHCircleMember alloc] init];
//    all.nickName = @"所有人";
//    //    all.autoId = -1;
//    all.qaInfo = @"发起提问后，圈内所有人能收到你的提问";
//    all.askCount = self.askCount;
//    MHAskItemViewModel *allViewModel =  [[MHAskItemViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:all}];
//    allViewModel.isAll = YES;
//    allViewModel.info = self.info;
    if (MHObjectIsNil(systemMessages) || systemMessages.count == 0) return @[];
    NSMutableArray *viewModels = [[systemMessages.rac_sequence map:^(MHSystemMessageModel *systemModel) {
        MHSystemMessageItemViewModel *viewModel = [[MHSystemMessageItemViewModel alloc] initWithModel:systemModel];
//        viewModel.info = self.info;
        return viewModel;
    }].array mutableCopy];
//    [viewModels appendObject:allViewModel];
    return viewModels ?: @[] ;
}

@end
